package tq.com.tqteacher.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.githang.statusbar.StatusBarCompat;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishItem;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQTBNewPublish;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.TQChoiceFileView;
import tq.com.tqteacher.view.TQHeader3ViewHold;
import tq.com.tqteacher.view.TQUploadFinishView;
import tq.com.tqteacher.view.viewhold.TQPublishResultViewHelper;
import tq.com.tqteacher.view.viewhold.TQPublishSaveButtonViewHold;

public class TQUploadFinishActivity extends BaseActivity {
    private TQHeader5View mHeader5View;
    private TQChoiceFileView choiceFileView;
    private TQUploadFinishView uploadFinishView;

    private TQPublishResultViewHelper mPublishResultViewHelper;
    private TQPublishSaveButtonViewHold mTQPublishSaveButtonViewHold;

    static private int requestCode = 0x11;

    static public void show(Activity pActivity){
        Intent lIntent = new Intent(pActivity, TQUploadFinishActivity.class);
        pActivity.startActivity(lIntent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tb_result_layout);
        setStatusBar();

        mHeader5View = findViewById(R.id.title);
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQUploadFinishActivity.this.finish();
            }
        });

        mHeader5View.getTitleView().setText(TQPublishType.title());

        choiceFileView = findViewById(R.id.choice_file);
        uploadFinishView = findViewById(R.id.upload_finish);

        uploadFinishView.setVisibility(View.GONE);
        choiceFileView.setFinishCall(new TQChoiceFileView.Callback() {
            @Override
            public void finishChoice() {
                gotoChoice();
            }
        });

        uploadFinishView.setreuploadListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoChoice();
            }
        });
        uploadFinishView.setShowPdfListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPdf();
            }
        });

        if(mPublishResultViewHelper == null){
            mPublishResultViewHelper = new TQPublishResultViewHelper(findViewById(R.id.baseInfo));
            mPublishResultViewHelper.setModifyClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onModify();
                }
            });
        }
        if(mTQPublishSaveButtonViewHold == null){
            mTQPublishSaveButtonViewHold = new TQPublishSaveButtonViewHold(findViewById(R.id.bottom));
            mTQPublishSaveButtonViewHold.setPublishClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onPublish();
                }
            });
        }

        if(TQPublishType.getSharePublishItem().getNeedloadRemote()){
//            getDetailFromServer();
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        if((TQPublishType.getSharePublishItem() instanceof TQTBNewPublish)){
            initData();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(resultCode != RESULT_OK){
            return;
        }

        if(requestCode == this.requestCode){
            String pdf = data.getStringExtra(TQPdfReaderActivity.RESULT_KEY);
            if(!StringHelper.isEmpty(pdf)){
                bindAttach(pdf);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initData(){
        TQTBNewPublish lNewPublish = (TQTBNewPublish) TQPublishType.getSharePublishItem();
        mPublishResultViewHelper.setTBResult(TQPublishType.getSharePublishItem());

        if(StringHelper.isEmpty(lNewPublish.getPdfFile())){
            // goto choice
            setStatus(0);
            if(!TQPublishType.getSharePublishItem().isModify()){
                gotoChoice();
                TQPublishType.getSharePublishItem().setModify(true);
            }

        }else{
            setStatus(1);
        }

        if(lNewPublish.isPublish()){
            mTQPublishSaveButtonViewHold.setAlreadyPublish();
        }
    }

    private void gotoChoice(){
        TQDirectoryActivity.showSelect(this, requestCode, TQSystemDefine.FILE_TYPE_PDF);
    }
    private void setStatus(int s){
        if(s == 0){
            choiceFileView.setVisibility(View.VISIBLE);
            uploadFinishView.setVisibility(View.INVISIBLE);

        }else{
            choiceFileView.setVisibility(View.INVISIBLE);
            uploadFinishView.setVisibility(View.VISIBLE);
        }
    }

    private void bindAttach(final String pdf){
        TQAtachModel lAtachModel = new TQAtachModel();
        lAtachModel.add(pdf);

        ArrayList<HashMap<String, String>> lMapArrayList = new ArrayList<>();
        lMapArrayList.add(lAtachModel.getHashInfo());

        TQNetInterface.bindAttach(TQPublishType.getSharePublishItem().getSeq_id(), lMapArrayList, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    AlertDialogHelper.show(TQUploadFinishActivity.this, pNetResult.getErrMsg());
                    return;
                }

                setStatus(1);

                if(TQPublishType.getSharePublishItem() instanceof TQTBNewPublish){
                    ((TQTBNewPublish)TQPublishType.getSharePublishItem()).setPdfFile(pdf);
                }
            }
        });

    }

    private void getDetailFromServer(){
//        TQNetInterface.getMatrialDetail(TQPublishType.getSharePublishItem().getSeq_id(), new INetCallback() {
//            @Override
//            public void result(TQNetResult pNetResult) {
//                if(!pNetResult.isSuccess()){
//                    AlertDialogHelper.show(TQUploadFinishActivity.this, pNetResult.getErrMsg());
//                    return;
//                }
//
//                if(pNetResult.getData() == null) return;
//
//                JSONObject lObject = pNetResult.getData().optJSONObject("material");
//                if(lObject == null) return;
//                JSONObject lBaseInfo = lObject.optJSONObject("material");
//                if(lBaseInfo == null) return;
//                int cate = lBaseInfo.optInt("category");
//                if(cate != TQPublishType.getSharePublishItem().getType()) return;
//
//                JSONArray lAttach = lObject.optJSONArray("attachment");
//                TQPublishType.getSharePublishItem().refreshAttach(lAttach);
//
//                initData();
//            }
//        });

        TQPublishType.getSharePublishItem().getDetailFromServer(this, new TQNewPublish.INetFinishListener() {
            @Override
            public void finish() {
                initData();
            }
        });
    }

    private void onModify(){
        Intent lIntent = new Intent(this, TQNewPublishFXJActivity.class);
        startActivity(lIntent);
    }
    private void showPdf(){
        if(TQPublishType.getSharePublishItem() instanceof TQTBNewPublish){
            String pdf = ((TQTBNewPublish)TQPublishType.getSharePublishItem()).getPdfFile();
            TQPdfReaderActivity.showRead(this, pdf);
        }
    }

    private void onPublish(){

        if(TQPublishType.getTQOpenFrom() == TQPublishType.TQOpenFrom.optimize){
            AlertDialogHelper.show(this, "已同步更新");
        }else{
            String pdf = ((TQTBNewPublish)TQPublishType.getSharePublishItem()).getPdfFile();
            if(StringHelper.isEmpty(pdf)){
                AlertDialogHelper.show(this, "请绑定资料文件");
                return;
            }

            TQPublishType.getSharePublishItem().onPublish(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQUploadFinishActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQPublishType.getSharePublishItem().setState(TQSystemDefine.PUBLISH_STATE_Publish);
                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.modify, null));
                    mTQPublishSaveButtonViewHold.setAlreadyPublish();

                    TQUploadFinishActivity.this.finish();
                }
            });
        }

    }
}
